<?php

namespace Unl\HomologacionBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Unl\HomologacionBundle\Entity\Usuario;
use Unl\HomologacionBundle\Entity\Persona;
use Unl\HomologacionBundle\Form\PersonaType;
use Unl\HomologacionBundle\Form\UsuarioType;
use Symfony\Component\HttpFoundation\Response;

/**
 * Usuario controller.
 *
 * @Route("/usuario")
 */
class UsuarioController extends BaseController
{
    /**
     * Lists all Usuario entities.
     *
     * @Route("/", name="usuario")
     * @Template()
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entities = $em->getRepository('UnlHomologacionBundle:Usuario')->findAll();

        return array('entities' => $entities);
    }

    /**
     * Finds and displays a Usuario entity.
     *
     * @Route("/{id}/show", name="usuario_show")
     * @Template()
     */
    public function showAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('UnlHomologacionBundle:Usuario')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Usuario entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),        );
    }

    /**
     * Displays a form to create a new Usuario entity.
     *
     * @Route("/new", name="usuario_new")
     * @Template()
     */
    public function newAction()
    {
        $entity = new Usuario();
        $usuarioType = new UsuarioType();
        $usuarioType->setOcultar(FALSE);
        $form   = $this->createForm($usuarioType, $entity);

        return array(
            'entity' => $entity,
            'edit_form'   => $form->createView()
        );
    }

    /**
     * Creates a new Usuario entity.
     *
     * @Route("/create", name="usuario_create")
     * @Method("post")
     * @Template("UnlHomologacionBundle:Usuario:new.html.twig")
     */
    public function createAction()
    {
        $entity  = new Usuario();
        $request = $this->getRequest();
        $usuarioType = new UsuarioType();
        $usuarioType->setOcultar(FALSE);
        $form    = $this->createForm($usuarioType, $entity);
        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $em->persist($entity);
            $em->flush();
            

            return $this->redirect($this->generateUrl('usuario_show', array('id' => $entity->getId())));
            
        }

        return array(
            'entity' => $entity,
            'edit_form'   => $form->createView()
        );
    }

    /**
     * Displays a form to edit an existing Usuario entity.
     *
     * @Route("/{id}/edit", name="usuario_edit")
     * @Template()
     */
    public function editAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('UnlHomologacionBundle:Usuario')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Usuario entity.');
        }
        
        $usuarioType = new UsuarioType();
        $usuarioType->setOcultar(TRUE);
        $editForm = $this->createForm($usuarioType, $entity);
        $deleteForm = $this->createDeleteForm($id);

        
        
        return new Response($entity->getClass().' '.$entity);
        
        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }

    /**
     * Edits an existing Usuario entity.
     *
     * @Route("/{id}/update", name="usuario_update")
     * @Method("post")
     * @Template("UnlHomologacionBundle:Usuario:edit.html.twig")
     */
    public function updateAction($id)
    { 
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('UnlHomologacionBundle:Usuario')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Usuario entity.');
        }

        $usuarioType = new UsuarioType();
        $usuarioType->setOcultar(TRUE);
        $editForm   = $this->createForm($usuarioType, $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('usuario_edit', array('id' => $id)));
        }

        
        
        
        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
         
    }

    /**
     * Deletes a Usuario entity.
     *
     * @Route("/{id}/delete", name="usuario_delete")
     * @Method("post")
     */
    public function deleteAction($id)
    {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();

        $form->bindRequest($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            $entity = $em->getRepository('UnlHomologacionBundle:Usuario')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Usuario entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('usuario'));
    }

    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden') 
            ->getForm()
        ;
    }
    
    /**
     *
     * @Route("/pedircontrasenia", name="usuario_pedircontrasenia")
     * @Template()
     */
    public function solicitarContraseniaAction()
    {
        $entity  = new Usuario();
        $request = $this->getRequest();
        $usuarioType = new UsuarioType();
        $form    = $this->createForm($usuarioType, $entity);
        $form->bindRequest($request);
        $criterio = $entity->getEmail();
        $em = $this->getDoctrine()->getEntityManager();
        $usuario= null;
        
        
        
        if ($criterio != null){
//             $usuario = $em->getRepository('UnlHomologacionBundle:Usuario')->find(15);
            $usuario = $em->getRepository('UnlHomologacionBundle:Usuario')->findByEmail($criterio);
            
        }
 
        
        if ($usuario != null){
            $LENGTH=8;
            $email= 'silvanavelez410@gmail.com';
//            $email= $usuario->getEmail();
            $asunto ='Solicitud de Nueva Contraseña';
            $contenido = ' Su nueva contraseña es: ';
            $nuevaClave='';
            for ($i=0;$i<$LENGTH;$i++) $nuevaClave.=chr(rand(65,90));
            $usuario->setPassword($nuevaClave);
            $usuario->setPlainPassword($nuevaClave);
            $em->persist($usuario);
            $em->flush();
            $this->enviarCorreo($email, $asunto,$contenido.$nuevaClave);
            return $this->redirect($this->generateUrl('login', array('descripcion' => 'se envio')));
             

            
        }else{
//           return new \Symfony\Component\HttpFoundation\Response("EL USUARIO NO ESTA REGISTRADO");
            
        }


            
        
        return array(
            'entity' => $entity,
            'form'   => $form->createView()
        );
        
    }
    



}